{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "D:\\ProgramData\\Anaconda3\\lib\\site-packages\\h5py\\__init__.py:34: FutureWarning: Conversion of the second argument of issubdtype from `float` to `np.floating` is deprecated. In future, it will be treated as `np.float64 == np.dtype(float).type`.\n",
      "  from ._conv import register_converters as _register_converters\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Extracting MNIST_data\\train-images-idx3-ubyte.gz\n",
      "Extracting MNIST_data\\train-labels-idx1-ubyte.gz\n",
      "Extracting MNIST_data\\t10k-images-idx3-ubyte.gz\n",
      "Extracting MNIST_data\\t10k-labels-idx1-ubyte.gz\n"
     ]
    }
   ],
   "source": [
    "from __future__ import absolute_import\n",
    "from __future__ import division\n",
    "from __future__ import print_function\n",
    "import argparse\n",
    "import sys\n",
    "import os\n",
    "os.chdir(\"E:\\pythonstudy\")\n",
    "from tensorflow.examples.tutorials.mnist import input_data\n",
    "import tensorflow as tf\n",
    "mnist = input_data.read_data_sets('MNIST_data', one_hot=True)\n",
    "FLAGS = None"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# holder变量\n",
    "x = tf.placeholder(tf.float32,[None,784])\n",
    "y_ = tf.placeholder(tf.float32,[None,10])\n",
    "\n",
    "#设置各层神经元个数\n",
    "w1 = tf.Variable(tf.truncated_normal([784,3600],stddev=0.1))\n",
    "b1 = tf.Variable(tf.constant(0,1),[3600])\n",
    "w2 = tf.Variable(tf.truncated_normal([3600,50],stddev=0.1))\n",
    "b2 = tf.Variable(tf.constant(0,1),[50])\n",
    "w3 = tf.Variable(tf.truncated_normal([50,10],stddev=0.1))\n",
    "b3 = tf.Variable(tf.constant(0,1),[10])\n",
    "\n",
    "#输入层\n",
    "y1 = tf.matmul(x, w1) + b1\n",
    "y1 = tf.nn.relu(y1)\n",
    "y2 = tf.matmul(y1, w2) + b2\n",
    "y2 = tf.nn.relu(y2)\n",
    "\n",
    "#输出层\n",
    "y = tf.matmul(y2, w3) + b3\n",
    "y = tf.nn.relu(y)\n",
    "\n",
    "#计算损失函数\n",
    "loss=tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=y_,logits=y))\n",
    "regularizer=tf.contrib.layers.l2_regularizer(0.0001)\n",
    "regularization=regularizer(w1)+regularizer(w2)+regularizer(w3)#正则化\n",
    "loss += regularization\n",
    "#优化器\n",
    "opt = tf.train.GradientDescentOptimizer(0.5).minimize(loss)\n",
    "correct_prediction = tf.equal(tf.argmax(y,1),tf.argmax(y_,1))       \n",
    "acuracy = tf.reduce_mean(tf.cast(correct_prediction,tf.float32))  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0 0.3\n",
      "1000 1.0\n",
      "2000 1.0\n",
      "3000 1.0\n",
      "4000 1.0\n",
      "0.982\n"
     ]
    }
   ],
   "source": [
    "#初始化变量\n",
    "sess=tf.Session()\n",
    "init=tf.global_variables_initializer()\n",
    "sess.run(init)\n",
    "for i in range(5000):\n",
    "    batch = mnist.train.next_batch(100)\n",
    "    batchInput = batch[0]\n",
    "    batchLabels = batch[1]\n",
    "    _,trainingloss = sess.run([opt,loss],feed_dict={x:batchInput,y_:batchLabels})\n",
    "    if i%1000==0:\n",
    "        trainAccurary = acuracy.eval(session=sess,feed_dict={x:batchInput,y_:batchLabels})\n",
    "        print(i, trainAccurary )\n",
    "\n",
    "print (acuracy.eval(session=sess,feed_dict={x:mnist.test.images,y_:mnist.test.labels}))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
